package www.my.imageframecompare;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import www.my.imageframecompare.PhotoPicker.SelectShowActivity;
import www.my.imageframecompare.VolleyConfig.BitmapCache;
import www.my.imageframecompare.drawableUtil.DrawableShowActivity;
import www.my.imageframecompare.multiImgContinue.MultiViewContinueActivity;
import www.my.imageframecompare.myGlide.GlideShow;
import www.my.imageframecompare.oom.BigImgActivity;

/**
 * 多图 轮训 展示
 */
public class MainActivity extends AppCompatActivity {

    private LinearLayout ll;
    private String bigimgePath;
    private ImageView photoview;
    private ImageView iv1;
    private ImageView iv2;
    private SimpleDraweeView iv3;
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll = (LinearLayout) findViewById(R.id.ll);
        rv = (RecyclerView) findViewById(R.id.rv);
        initView();
    }

    private void initView() {
        final List<TempSave> mList = new ArrayList();
        mList.add(new TempSave("view 缓存展示 ", MultiViewContinueActivity.class));
        MainAdapter mainAdapter = new MainAdapter(mList);
        // StaggeredGridLayoutManager:流式布局,例如瀑布流效果
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        rv.setLayoutManager(staggeredGridLayoutManager);
        rv.setAdapter(mainAdapter);
        mainAdapter.setOnItemClickListener(new MainAdapter.ItemClickListener() {
            @Override
            public void itemClickListener(int position) {
                startActivity(new Intent(MainActivity.this,mList.get(position).getData()));
            }
        });
        BitmapCache bitmapCache = new BitmapCache(this);
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        final ImageLoader imageLoader = new ImageLoader(requestQueue, bitmapCache);
//        1920*1200  168KB
//        bigimgePath = "http://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=http%3A%2F%2Fimg2.3lian.com%2F2014%2Ff6%2F173%2Fd%2F51.jpg&thumburl=http%3A%2F%2Fe.hiphotos.baidu.com%2Fimage%2Fh%253D200%2Fsign%3D31e61d6532f33a87816d071af65d1018%2F95eef01f3a292df504213240b4315c6035a87381.jpg";
        bigimgePath=Config.pic_Internet;
        photoview = (ImageView) findViewById(R.id.photoview);
        iv1 = (ImageView) findViewById(R.id.image1);
        iv2 = (ImageView) findViewById(R.id.image2);
        iv3 = (SimpleDraweeView) findViewById(R.id.image3);
        Button button = new Button(this);
        button.setText("加载大图");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //imageloader  volley
                ImageLoader.ImageListener listener = ImageLoader.getImageListener(photoview, R.drawable.image_loading, R.drawable.image_error);
                imageLoader.get(bigimgePath, listener);
                ImageLoader.ImageListener listener2 = ImageLoader.getImageListener(iv1, R.drawable.image_loading, R.drawable.image_error);
                imageLoader.get(bigimgePath, listener2);
                //glide
                Glide.with(MainActivity.this)
                        .load(bigimgePath)
                        .placeholder(R.drawable.image_loading)
                        .error(R.drawable.image_error)
                        .into(iv2);
                //Fresco
                GenericDraweeHierarchy hierarchy = iv3.getHierarchy();
                hierarchy.setPlaceholderImage(R.drawable.image_loading);
//                hierarchy.setFailureImage(R.mipmap.icon_failure);
                hierarchy.setFailureImage(R.drawable.image_error);
                hierarchy.setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER);
                RoundingParams roundingParams = hierarchy.getRoundingParams();
//                roundingParams.setCornersRadius(10);
//                hierarchy.setProgressBarImage(R.mipmap.icon_progress_bar);
//                hierarchy.setProgressBarImage(R.mipmap.icon_progress_bar, ScalingUtils.ScaleType.FOCUS_CROP);
                hierarchy.setProgressBarImage(new ProgressBarDrawable());
//                hierarchy.set
//                hierarchy.
                hierarchy.setRoundingParams(roundingParams);
//                iv3.setHierarchy(hierarchy);
                iv3.setImageURI(bigimgePath);
            }
        });
        ll.addView(button, 0);
        Button button22 = new Button(this);
        button22.setText("ImageLoader测试加载大图");
        //4095*2365 800KB
        final String imgMemory = "http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fb.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Ff636afc379310a5562bec3ceb64543a982261075.jpg&thumburl=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D3613623735%2C596378317%26fm%3D21%26gp%3D0.jpg";
        ll.addView(button22, 1);
        button22.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ImageLoaderShow.class);
                intent.putExtra("img", imgMemory);
                startActivity(intent);
            }
        });
        Button button33 = new Button(this);
        button33.setText("Glide测试加载大图");
        //4095*2365 800KB
        ll.addView(button33, 2);
        button33.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, GlideShow.class);
                intent.putExtra("img", imgMemory);
                startActivity(intent);
            }
        });
        Button button4 = new Button(this);
        button4.setText("Fresco测试加载大图");
        //4095*2365 800KB
        ll.addView(button4, 3);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, FrescobaseShow.class);
                intent.putExtra("img", imgMemory);
                startActivity(intent);
            }
        });
        Button button42 = new Button(this);
        button42.setText("Fresco测试1");
        //4095*2365 800KB
        ll.addView(button42, 4);
        button42.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, FrescoShow.class);
                intent.putExtra("img", imgMemory);
                startActivity(intent);
            }
        });
        Button button5 = new Button(this);
        button5.setText("Fresco测试2");
        //4095*2365 800KB
        ll.addView(button5, 5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, FrescoTwoShow.class);
                intent.putExtra("img", imgMemory);
                startActivity(intent);
            }
        });
        Button button6 = new Button(this);
        button6.setText("相册选择");
        //4095*2365 800KB
        ll.addView(button6, 6);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SelectShowActivity.class);
                intent.putExtra("img", imgMemory);
                startActivity(intent);
            }
        });

        Button button7= new Button(this);
        button7.setText("保存截图到本地并且系统图库显示");
        //4095*2365 800KB
        ll.addView(button7, 7);
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, PrintScreenSave.class);
                intent.putExtra("img", imgMemory);
                startActivity(intent);
            }
        });

        Button button00 = new Button(this);
        ll.addView(button00, 0);
        button00.setText("Drawables的高效用法");
        button00.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, DrawableShowActivity.class));
            }
        });
        Button button01 = new Button(this);
        ll.addView(button01, 0);
        button01.setText("BigImg避免oom");
        button01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, BigImgActivity.class));
            }
        });
    }

    class TempSave {
        String name;
        Class<?>data;

        public TempSave(String name, Class<?> data) {
            this.name = name;
            this.data = data;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Class<?> getData() {
            return data;
        }

        public void setData(Class<?> data) {
            this.data = data;
        }
    }
}
